#include<stdio.h>
#include<stdlib.h>
#include<string.h>

#define hour(t) ((t[0]*10) + t[1])
#define minute(t) ((t[3]*10) + t[4])
#define second(t) ((t[6]*10) + t[7])

int compare(const char * t1, const char * t2)
{     
     long x1 = hour(t1)*10000 + minute(t1)*100 + second(t1);
     long x2 = hour(t2)*10000 + minute(t2)*100 + second(t2);
     if (x1 == x2)
        return 0;
     else if (x1 < x2)
        return -1;
     else
        return 1;
}

int main()
{
    int days;
    scanf("%d", &days);
    while(days--)
    {
           int records;
           scanf("%d", &records);
           int i = 0;
           char id[16];
           char intime[9];
           char outtime[9];
           
           char minin[9] = "23:59:59";
           char maxout[9] = "00:00:00";
           char minid[16];
           char maxid[16];
           
           for(; i < records; ++i)
           {
                 scanf("%s%s%s", id, intime, outtime);
                 if(compare(intime, minin) == -1)
                 {
                        strcpy(minin, intime);
                        strcpy(minid, id);
                 }
                 if(compare(outtime, maxout) == 1)
                 {
                        strcpy(maxout, outtime);
                        strcpy(maxid, id);                                 
                 }       
           }
           
           printf("%s %s\n", minid, maxid);             
    }
    
    system("pause");
    return 0;    
}
